package com.android.fragmentdemo
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.fragmentdemo.adapters.FooterBodyRVAdapter
import com.android.fragmentdemo.adapters.HeaderBodyRVAdapter
import com.android.fragmentdemo.adapters.HeaderFooterBodyRVAdapter
import com.android.fragmentdemo.adapters.HeaderSpanBodyRVAdapter
import com.android.fragmentdemo.adapters.NormalRvAdapter
import com.hengfoxwolf.commonbase.base.BaseActivity2
import com.android.fragmentdemo.databeans.DataFactoryIp
import com.android.fragmentdemo.databinding.ActivityRvwithHeaderBinding
import com.android.fragmentdemo.defineviews.Dy01ItemDecoration
import com.android.fragmentdemo.defineviews.DySpanItemDecoration

class RVWithHeaderActivity :
    BaseActivity2<ActivityRvwithHeaderBinding>(ActivityRvwithHeaderBinding::inflate) {

    override fun init(savedInstanceState: Bundle?) {

        val dyAdapter = HeaderBodyRVAdapter(resources.getString(R.string.txt_start_view))
        dyAdapter.itemDataSet = DataFactoryIp.createCharStr(15)
        mBinding.rvWithHeader.apply {
            layoutManager = LinearLayoutManager(
                this@RVWithHeaderActivity,
                LinearLayoutManager.HORIZONTAL,
                false
            )
            adapter = dyAdapter
            addItemDecoration(
                Dy01ItemDecoration(
                    this@RVWithHeaderActivity.resources.getDimension(
                        R.dimen.dp_10
                    )
                )
            )
        }

        val dy1Adapter = FooterBodyRVAdapter(resources.getString(R.string.txt_ending_view))
        //dy1Adapter.itemDataSet = DataFactoryIp.createCharStr(20)
        dy1Adapter.setItemDataSet(DataFactoryIp.createCharStr(20))
        mBinding.rvWithFooter.apply {
            layoutManager = LinearLayoutManager(
                this@RVWithHeaderActivity,
                LinearLayoutManager.HORIZONTAL,
                false
            )
            adapter = dy1Adapter
            addItemDecoration(
                Dy01ItemDecoration(
                    this@RVWithHeaderActivity.resources.getDimension(
                        R.dimen.dp_10
                    )
                )
            )
        }

        val dy2Adapter = HeaderFooterBodyRVAdapter(resources.getString(R.string.txt_start_view),resources.getString(R.string.txt_ending_view))
        //dy2Adapter.itemDataSet = DataFactoryIp.createCharStr(22)
        dy2Adapter.setItemDataSet(DataFactoryIp.createCharStr(22))
        mBinding.rvWithHeaderFooter.apply {
            layoutManager = LinearLayoutManager(
                this@RVWithHeaderActivity,
                LinearLayoutManager.HORIZONTAL,
                false
            )
            adapter = dy2Adapter
            addItemDecoration(
                Dy01ItemDecoration(
                    this@RVWithHeaderActivity.resources.getDimension(
                        R.dimen.dp_10
                    )
                )
            )
        }

        val dy3Adapter = NormalRvAdapter()
        dy3Adapter.setItemDataSet(DataFactoryIp.createCharStr(22))
        mBinding.rvNormal.apply {
            layoutManager = LinearLayoutManager(
                this@RVWithHeaderActivity,
                LinearLayoutManager.HORIZONTAL,
                false
            )
            adapter = dy3Adapter
            addItemDecoration(
                Dy01ItemDecoration(
                    this@RVWithHeaderActivity.resources.getDimension(
                        R.dimen.dp_10
                    )
                ).apply {
                    setFirstItemSpace(true)
                }
            )
        }

        Handler(Looper.getMainLooper()).postDelayed({
          //dy2Adapter.setItemDataSet(DataFactoryIp.createCharStr(5),false)
          //dy2Adapter.removeItemDataSet()
          //dy2Adapter.addItemData("W5")
          //dy2Adapter.addItemData("W5", 2)
          //dy2Adapter.addItemData("W5", 0)
          //dy2Adapter.removeItemData("B2")
          //dy2Adapter.removeItemData(1)

            dy1Adapter.setItemDataSet(DataFactoryIp.createCharStr(5), true)
            // 问题点:插入数据后没有动画,也没有显示插入的数据,需要向右滑动才显示.
            // 方案:暂时未解决 需要滚动到第一个item位置的话 可以调用scrollToPosition方法或者调用notifyDataSetChanged()
            //mBinding.rvWithFooter.scrollToPosition(0)

            //dy1Adapter.removeItemDataSet()
            //dy1Adapter.addItemData("W5")
            //dy1Adapter.addItemData("W5", 2)

            //dy1Adapter.addItemData("W5", 0)
            // 问题点:插入数据后没有动画,也没有显示插入的数据,需要向右滑动才显示.
            // 方案:暂时未解决 需要滚动到第一个item位置的话 可以调用scrollToPosition方法或者调用notifyDataSetChanged()
            //mBinding.rvWithFooter.scrollToPosition(0)

            //dy1Adapter.removeItemData("B2")
            //dy1Adapter.removeItemData(1)

            //dyAdapter.setItemDataSet(DataFactoryIp.createCharStr(5),true)
            //dyAdapter.removeItemDataSet()
            //dyAdapter.addItemData("W5")
            //dyAdapter.addItemData("W5", 2)
            //dyAdapter.addItemData("W5", 0)
            //dyAdapter.removeItemData("B2")
            //dyAdapter.removeItemData(1)

            //dy3Adapter.setItemDataSet(DataFactoryIp.createCharStr(5),false)
            //dy3Adapter.removeItemDataSet()
            //dy3Adapter.addItemData("W5")
            //dy3Adapter.addItemData("W5", 2)
            //dy3Adapter.addItemData("W5", 0)
            //dy3Adapter.removeItemData("B2")
            //dy3Adapter.removeItemData(1)
            //dy3Adapter.removeItemData(0)
        }, 5000)

        val dy4Adapter = HeaderSpanBodyRVAdapter(resources.getString(R.string.txt_start_view))
        dy4Adapter.itemDataSet = DataFactoryIp.createCharStr(15)
        val spanCount = 4
        mBinding.rvSpanWithHeader.apply {
            layoutManager = GridLayoutManager(this@RVWithHeaderActivity, spanCount).apply {
                spanSizeLookup = object : SpanSizeLookup()  {
                    override fun getSpanSize(position: Int): Int {
                        return if (position == 0) spanCount else 1
                    }
                }
            }
            adapter = dy4Adapter
            addItemDecoration(
                DySpanItemDecoration(
                    this@RVWithHeaderActivity.resources.getDimension(R.dimen.dp_10),
                    spanCount
                )
            )
        }

        val dy5Adapter = HeaderSpanBodyRVAdapter(resources.getString(R.string.txt_start_view))
        dy5Adapter.itemDataSet = DataFactoryIp.createCharStr(15)
        val spanCount2 = dy5Adapter.itemDataSet!!.size
        mBinding.rvSpanWithHeader2.apply {
            layoutManager = GridLayoutManager(this@RVWithHeaderActivity, spanCount2).apply {
                spanSizeLookup = object : SpanSizeLookup()  {
                    override fun getSpanSize(position: Int): Int {
                        return if (position == 0) spanCount2 else 1
                    }
                }
            }
            adapter = dy5Adapter
            addItemDecoration(
                DySpanItemDecoration(
                    this@RVWithHeaderActivity.resources.getDimension(R.dimen.dp_10),
                    spanCount2
                )
            )
        }

    }

}